package com.swiftops.basic.sys.controller;

import com.swiftops.basic.sys.service.SysLoginLogService;
import com.swiftops.basic.sys.vo.entity.SysLoginLogEntityVO;
import com.swiftops.basic.sys.vo.request.SysLoginLogRequestVO;
import com.swiftops.basic.sys.vo.response.SysLoginLogResponseVO;
import com.swiftops.common.core.ApiResult;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录日志
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "登录日志")
public class SysLoginLogController {

    @Autowired
    private SysLoginLogService sysLoginLogService;

    @ApiOperation("获取登录日志分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysLoginLog", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageResult<SysLoginLogResponseVO>>> getSysLoginLogPageList(SysLoginLogRequestVO requestVO) {
        try {
            PageResult<SysLoginLogResponseVO> pageResult = this.sysLoginLogService.querySysLoginLogPageList(requestVO);
            return ApiResult.ok(pageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取登录日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysLoginLog/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<SysLoginLogEntityVO>> getSysLoginLog(@PathVariable("id") String id) {
        try {
            SysLoginLogEntityVO sysLoginLogEntityVO = this.sysLoginLogService.getSysLoginLogEntityVO(id);
            return ApiResult.ok(sysLoginLogEntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增登录日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysLoginLog", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> addSysLoginLog(@RequestBody @Validated SysLoginLogEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysLoginLogService.addSysLoginLog(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新登录日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysLoginLog", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> updateSysLoginLog(@RequestBody @Validated SysLoginLogEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysLoginLogService.updateSysLoginLog(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }


    @ApiOperation("删除登录日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysLoginLog/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> deleteSysLoginLog(@PathVariable("id") String id) {
        try {
            this.sysLoginLogService.deleteSysLoginLog(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
